package org.jawp.service.aws;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

public class Aws {
	
	/*
     * Your AWS Access Key ID, as taken from the AWS Your Account page.
     */
    private String AWS_ACCESS_KEY_ID;

    private String AWS_SECRET_KEY;
    
    private String AWS_ASSOCIATED_TAG;
    
    private String ENDPOINT;
    
    /*
     * Use one of the following end-points, according to the region you are
     * interested in:
     * 
     *      US: ecs.amazonaws.com 
     *      CA: ecs.amazonaws.ca 
     *      UK: ecs.amazonaws.co.uk 
     *      DE: ecs.amazonaws.de 
     *      FR: ecs.amazonaws.fr 
     *      JP: ecs.amazonaws.jp
     * 
     */

    private SignedRequestsHelper helper;
    
    private Map<String, String> params;
    private String paramService;
    private String paramVersion;
    private String paramOperation;
    private String paramResponseGroup;
    private String paramSearchIndex;
    
    public Aws(){
    	AWS_ACCESS_KEY_ID = "AKIAI45NN7FD7O7Y3G5Q";
    	AWS_SECRET_KEY = "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";
    	AWS_ASSOCIATED_TAG = "javamawebstop-20";
    	ENDPOINT = "ecs.amazonaws.com";
    	
    	try {
           helper = SignedRequestsHelper.getInstance(ENDPOINT, AWS_ACCESS_KEY_ID, AWS_SECRET_KEY, AWS_ASSOCIATED_TAG);
        }
    	catch (Exception e) {
           e.printStackTrace();
           return;
        }
    	
    	/*
    	 * Set the default params
    	 */
        paramService = "AWSECommerceService";
        paramVersion = "2011-08-01";
        paramOperation = "ItemSearch";
        //paramResponseGroup = "Small";
        paramResponseGroup = "ItemAttributes,Offers,Images,Reviews";
        paramSearchIndex = "All";
        params =  new HashMap<String, String>();
    }
    
    public void setSearchIndex(String searchIndex){
    	paramSearchIndex = searchIndex;
    }
    
    public void setOperation(String searchIndex){
    	paramOperation = searchIndex;
    }
    
    public void prepareParams(){
    	params.clear();
    	params.put("Service", paramService);
        params.put("Version", paramVersion);
        params.put("Operation", paramOperation);
        params.put("ResponseGroup", paramResponseGroup);
        params.put("SearchIndex", paramSearchIndex);
    }
    
    public void prepareParams(Map<String, String> otherParams){
    	params.clear();
    	params.put("Service", paramService);
        params.put("Version", paramVersion);
        params.put("Operation", paramOperation);
        params.put("ResponseGroup", paramResponseGroup);
        params.put("SearchIndex", paramSearchIndex);
        
        for(Iterator<String> param = otherParams.keySet().iterator(); param.hasNext();) {
        	String key = param.next();
        	String value = otherParams.get(key);
			params.put(key, value);
		}
    }
    
    public Document requestURL(String requestUrl)
    {
    	try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(requestUrl);
            return doc;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    
    public List<Map<String, String>> findArticleByItemID(String itemID){
    	List<Map<String, String>> article = null;
    	Map<String, String> otherParams = new HashMap<String, String>();
    	otherParams.put("ItemId", itemID);
        
    	//we set up the correct operation
        setOperation("ItemLookup");
    	
    	prepareParams(otherParams); 	
    	
    	//When IdType equals ASIN, SearchIndex cannot be present
        params.remove("SearchIndex");
        
    	String requestUrl = helper.sign(params);
    	Document doc = requestURL(requestUrl);
    	
    	System.out.println("Query: " + requestUrl);
    	
    	article = ParserXML.findListItem(doc);
    	
    	return article;
    }   
    
    public List<Map<String, String>> findArticleByKeyword(String keyword){
    	List<Map<String, String>> article = null;
    	Map<String, String> otherParams = new HashMap<String, String>();
    	otherParams.put("Keywords", keyword);

    	//we set up the correct operation
        setOperation("ItemSearch");
        
    	prepareParams(otherParams);
    	String requestUrl = helper.sign(params);
    	Document doc = requestURL(requestUrl);
    	
    	System.out.println("Query: " + requestUrl);
    	
    	article = ParserXML.findListItem(doc);
    	    	
    	return article;
    }   
    
    public List<Map<String, String>> findArticleByKeyword(String keyword, String searchIndex){
    	/* searchIndex doit être parmi les listes
    	 * Book, Classica, DVD, Electronics, ForeignBooks, ...
    	 */
    	List<Map<String, String>> article = null;
    	Map<String, String> otherParams = new HashMap<String, String>();
    	otherParams.put("Keywords", keyword);
    	otherParams.put("SearchIndex", searchIndex);

    	//we set up the correct operation
        setOperation("ItemSearch");
        
    	prepareParams(otherParams);
    	String requestUrl = helper.sign(params);
    	Document doc = requestURL(requestUrl);
    	
    	System.out.println("Query: " + requestUrl);
    	
    	article = ParserXML.findListItem(doc);
    	    	
    	return article;
    }

}
